import json

from utils.randomUtils import all_combinations


def paresJsonObject(filePath):
    with open(filePath, 'r') as file:
        # 读取JSON文件并转换为Python字典
        return json.load(file)



def calClientsContribution(clientNum,comAcc):
    """

    :param clientNum: 客户端数
    :param comAcc: 各种客户端组合的模型的准确率,是一个json字符串
    :return:
    """
    contributions = [0.0 for i in range(clientNum)]
    clientIds = list(range(clientNum))
    combinations = all_combinations(clientIds)
    for combination in combinations:
        if len(combination) == 1:
            key = str(combination)
            contributions[combination[0]] += comAcc[key]
            continue
        for clientId in combination:
            com_set = set(combination)
            com_set.remove(clientId)
            key1 = str(combination)
            key2 = str(tuple(sorted(list(com_set))))
            contributions[clientId] += max(comAcc[key1]-comAcc[key2],0)
    return contributions

def calClientsContributionMR(clientNum,comAcc):
    """
    :param clientNum: 客户端数
    :param comAcc: 各种客户端组合的模型的准确率,是一个json字符串
    :return:
    """
    contributions = [0.0 for i in range(clientNum)]
    clientIds = list(range(clientNum))
    combinations = all_combinations(clientIds)
    a = 1.0
    for combination in combinations:
        if len(combination) == 1:
            # key = str(combination)
            # contributions[combination[0]] += comAcc[key]
            continue
        for clientId in combination:
            com_set = set(combination)
            com_set.remove(clientId)
            key1 = str(combination)
            key2 = str(tuple(sorted(list(com_set))))
            contributions[clientId] += max(comAcc[key1]-comAcc[key2],0)
    return contributions

# def cal


def percentageOfTotal(lst):
    total_sum = sum(lst)
    return [100 * (x / total_sum) for x in lst]

def addList(targetList, tempList):
    for i in range(len(tempList)):
        targetList[i]+=tempList[i]
    return targetList